package com.cola.service;

import com.cola.dao.CommonDao;
import com.cola.dao.MepDao;
import com.cola.entity.export.ExportMep;
import com.cola.enums.CommonStatus;
import com.cola.enums.ImportType;
import com.cola.utils.baiduMap.BaiduMapUtils;
import com.cola.utils.baiduMap.entity.BaiduMapResult;
import com.cola.utils.excel.ExcelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MepService
 * @author Max
 * @since 2017/9/6
 */
public class MepService {
    private static volatile MepService instance = null;
    private final Logger logger = LoggerFactory.getLogger(getClass());
    public static MepService getInstance() {
        if (instance == null) {
            synchronized (MepService.class) {
                if (instance == null) {
                    instance = new MepService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取经销商列表
     * @param beginNum 搜索起始位置
     * @param pageSize 页面大小
     * @param filter 搜索条件
     * @return List<Map<String,Object>> 经销商信息列表
     * @author Max
     * @since 2017/9/6
     */
    public List<Map<String,Object>> getMepList(Integer beginNum,Integer pageSize,String filter){
        return MepDao.getInstance().getMepList(beginNum,pageSize,filter);
    }

    /**
     * 获取经销商数量
     * @param filter 搜索条件
     * @return Integer数量
     * @author Max
     * @since 2017/9/6
     */
    public Integer getMepCount(String filter){
        return MepDao.getInstance().getMepCount(filter);
    }

    /**
     * 根据id获取Mep详细信息
     * @param mepId 经销商内部id
     * @return  Map<String,Object> 经销商信息
     * @author Max
     * @since 2017/9/6
     */
    public Map<String,Object> getMepInfoById(Integer mepId){
        return MepDao.getInstance().getMepInfoById(mepId);
    }

    /**
     * 保存Mep信息
     * @param id 经销商id
     * @param name 经销商名称
     * @param mobile 经销商手机号
     * @param contact_name 经销商联系人
     * @param address 经销商地址
     * @param city 经销商城城市
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/6
     */
    public boolean saveMepInfo(Integer id,String name,String mobile,String contact_name,String address,String city){
        BaiduMapResult result = BaiduMapUtils.getLocationInfoByAddress(address);
        Double mep_lat = 0.0;
        Double mep_lng =0.0;
        try{
             mep_lat = Double.parseDouble(result.getResult().getLocation().getLat().toString());

             mep_lng = Double.parseDouble(result.getResult().getLocation().getLng().toString());

        }catch (Exception e){
            logger.error("Mep地址经纬度获取异常！",e);
        }
        return MepDao.getInstance().saveMepInfo( id, name, mobile, contact_name, address, city, mep_lat, mep_lng);
    }

    /**
     * 导入Mep信息
     * @param fileUrl 文件Url
     * @return  Map<String,Integer> 导入返回结果
     * @author Max
     * @since 2017/9/6
     */
    public Map<String,Integer> importMepInfo(String fileUrl,String username,String filename,Long userId){
        Integer importStatus = -1;
        Integer count = 0;
        Map<String,Integer> map = new HashMap<String, Integer>();
        Integer successCount = 0;
        Integer failCount = 0;
        try{
            List<List<String>> mepList = ExcelUtils.readExcel(fileUrl);
            if(mepList != null){
                count = mepList.size();
                for (List<String> list : mepList){
                    if(importMepInfo(list)){
                        successCount++;
                    }else{
                        failCount++;
                    }
                }
            }

            importStatus=1;
        }catch (Exception e){
            logger.error("导入Mep信息异常！",e);
        }finally {
            CommonDao.getInstance().addImportLog(userId,ImportType.MEP_IMPORT.getDesc(),filename,fileUrl,username,
                    count,successCount,failCount,importStatus,"");
        }
        map.put("successCount",successCount);
        map.put("failCount",failCount);
        return map;
    }


    /**
     * 导入Mep信息
     * @param list 解析Excel列表信息
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/6
     */
    public boolean importMepInfo(List<String> list){
        Map<String,Object> mepInfo = getMepInfoByList(list);
        if(mepInfo!=null){
            //解析地址
            String address = mepInfo.get("address").toString();

            BaiduMapResult locationResult = BaiduMapUtils.getLocationInfoByAddress(address);

            if(locationResult!=null){
                mepInfo.put("mep_lng",locationResult.getResult().getLocation().getLng());
                mepInfo.put("mep_lat",locationResult.getResult().getLocation().getLat());

                String mep_no = mepInfo.get("mep_no").toString();
                Integer status = MepDao.getInstance().checkMepNo(mep_no);
                //查询该经销商是否存在
                if (status == 0) {
                    //不存在 插入
                    return MepDao.getInstance().addMepInfo(mepInfo);
                } else{
                    //存在 更新
                    return status > 0 && MepDao.getInstance().updateMepInfo(mepInfo);
                }
            }
        }
        return false;
    }

    /**
     * 根据List获取Mep信息
     * @param List<String> list 解析Excel列表信息
     * @return Map<String,Object> 经销商信息
     * @author Max
     * @since 2017/9/6
     */
    private Map<String,Object> getMepInfoByList(List<String> list){
        try{
            Map<String,Object> mepInfo = new HashMap<String,Object>();
            mepInfo.put("mep_no",list.get(0));
            mepInfo.put("mep_name",list.get(1));
            mepInfo.put("city",list.get(2));
            mepInfo.put("address",list.get(3));
            mepInfo.put("mep_mobile",list.get(4));
            mepInfo.put("mep_contact_name",list.get(5));
            return mepInfo;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 删除经销商
     * @param mepId 经销商内部id
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/6
     */
    public boolean deleteMep(Integer mepId){
        return MepDao.getInstance().updateStatusMep(mepId, CommonStatus.DELETE.getDesc());
    }

    /**
     * 禁封经销商
     * @param mepId 经销商内部id
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/6
     */
    public boolean stopMep(Integer mepId){
        return MepDao.getInstance().updateStatusMep(mepId, CommonStatus.STOP.getDesc());
    }

    /**
     * 启用经销商
     * @param mepId 经销商内部id
     * @return true 成功;false 失败
     * @author Max
     * @since 2017/9/6
     */
    public boolean startMep(Integer mepId){
        return MepDao.getInstance().updateStatusMep(mepId, CommonStatus.NORMAL.getDesc());
    }


    /**
     * 获取经销商导入记录
     * @param offset 起始位置
     * @param limit 页面大小
     * @return  List<Map<String,Object>> 经销商信息列表
     * @author Max
     * @since 2017/9/6
     */
    public List<Map<String,Object>> getMepImportRecord(Integer offset,Integer limit){
        return MepDao.getInstance().getImportRecordList(offset,limit);
    }

    /**
     * 获取导入经销商记录数
     * @return Integer 记录数
     * @author Max
     * @since 2017/9/6
     */
    public Integer getMepImportRecordCount(){
        return MepDao.getInstance().getMepImportRecordCount();
    }


    /**
     * 获取导出MEP信息列表
     * @return  List<ExportMep> 经销商列表
     * @author Max
     * @since 2017/9/6
     */
    public List<ExportMep> getExportMepList(){
        return analysisExportMepList(MepDao.getInstance().getExportMepList());
    }

    /**
     * 处理Mep状态信息
     * @param List<ExportMep> list
     * @return List<ExportMep> list
     * @author Max
     * @since 2017/9/6
     */
    private List<ExportMep> analysisExportMepList(List<ExportMep> list){
        for (ExportMep mep : list){
            if(mep.getStatus()==1){
                //开启
                if(mep.getIs_bind()==1){
                    mep.setMep_status(1);
                }else{
                    mep.setMep_status(0);
                }
            }else{
                mep.setMep_status(-1);
            }
        }
        return list;
    }

    public static void main(String[] args) {
    }

}
